﻿using BLL;
using Entity;
using System;
using System.Data;
using System.Text;
using Web.Class;

namespace Web.Pages.YongShui._7.QiTaGL
{
    public partial class GongZuoRiGL : System.Web.UI.Page
    {
        #region 属性

        /// <summary>
        /// 年
        /// </summary>
        private int year
        {
            get
            {
                return Convert.ToInt32(ViewState["_year"]);
            }
            set
            {
                ViewState["_year"] = value;
            }
        }

        /// <summary>
        /// 月
        /// </summary>
        private int month
        {
            get
            {
                return Convert.ToInt32(ViewState["_month"]);
            }
            set
            {
                ViewState["_month"] = value;
            }
        }

        /// <summary>
        /// 日
        /// </summary>
        private int day
        {
            get
            {
                return Convert.ToInt32(ViewState["_day"]);
            }
            set
            {
                ViewState["_day"] = value;
            }
        }
        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtThisMonth
        {
            get
            {
                return ViewState["dtThisMonth"] as DataTable;
            }
            set
            {
                ViewState["dtThisMonth"] = value;
            }
        }

        #endregion
       
        #region 属性
        /// <summary>
        /// 工作日管理
        /// </summary>
        YS_QT_GONGZUORGL _YS_QT_GONGZUORGL = new YS_QT_GONGZUORGL();

        /// <summary>
        /// 工作日管理BLL
        /// </summary>
        YS_QT_GONGZUORGLBLL _YS_QT_GONGZUORGLBLL = new YS_QT_GONGZUORGLBLL();
        #endregion



        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDate();
            }
        }
        /// <summary>
        /// 年改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 月改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string days = hidStr.Value;
            int result = _YS_QT_GONGZUORGLBLL.UpdateYS_QT_GONGZUORGLsDynamic("i_sfgzr=1-i_sfgzr",
                    string.Format("i_year={0} and i_month={1} and i_day in ({2})", cmbYear.Text, cmbMonth.Text, days));
            if (result > 0)
            {
                PageTools.AlertMsg(this, "保存成功！");
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
            LoadData();
        }

        /// <summary>
        /// 放弃更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region 方法
        /// <summary>
        /// 初始化页面
        /// </summary>
        private void InitDate()
        {
            this.year = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
            this.month = Convert.ToInt32(DateTime.Now.ToString("MM"));
            this.day = Convert.ToInt32(DateTime.Now.ToString("dd"));
            
            for (int i = this.year; i <= 2026; i++)
            {
                cmbYear.Items.Add(i.ToString());
            }
            cmbYear.SelectedIndex = 0;

            for (int i = 1; i <= 12; i++)
            {
                cmbMonth.Items.Add(i.ToString());
            }
            cmbMonth.SelectedIndex = this.month - 1;
            LoadData();
        }

        /// <summary>
        /// 根据页面条件加载数据
        /// </summary>
        private void LoadData()
        {
            int c_year = Convert.ToInt32(cmbYear.Text);
            int c_month = Convert.ToInt32(cmbMonth.Text);
            this.dtThisMonth = _YS_QT_GONGZUORGLBLL.SelectYS_QT_GONGZUORGLsDynamic(string.Format("i_year={0} and i_month={1}  order by d_date", c_year, c_month), null);
            CreateTable();
        }

        /// <summary>
        /// 绘制日历
        /// </summary>
        private void CreateTable()
        {
            content1.InnerHtml = "";
            StringBuilder sb = new StringBuilder();
            sb.Append("<table id='Content' border='0'>");
            sb.Append("<tr class='head'><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>");
            
            int totalCount = this.dtThisMonth.Rows.Count;//总天数
            int firstWeek = Convert.ToInt32(this.dtThisMonth.Rows[0]["I_WEEKDAY"]);//1号是几
            int rows = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalCount + firstWeek) / 7));
            int row = 1;
            string td_className = "workday";//td样式名称 默认工作日
            for (int tr = 1; tr <= 6; tr++)
            {
                sb.Append("<tr>");
                for (int td = 1; td <= 7; td++)
                {
                    if (tr == 1 && td < firstWeek)
                    {
                        sb.Append("<td class='empty'></td>");
                    }
                    else
                    {
                        if (row <= totalCount)
                        {
                            td_className = Convert.ToInt32(this.dtThisMonth.Rows[row - 1]["I_SFGZR"]) == 1 ? "workday" : "noworking";
                            if (Convert.ToInt32(this.dtThisMonth.Rows[row - 1]["I_DAY"]) == this.day && Convert.ToInt32(this.dtThisMonth.Rows[row - 1]["I_MONTH"]) == this.month && Convert.ToInt32(this.dtThisMonth.Rows[row - 1]["I_YEAR"]) == this.year)
                            {
                                td_className += " NowDay";
                            }
                            sb.Append("<td class='" + td_className + "' onclick='ChangeColor(this);' flag='0'>" + Convert.ToInt32(this.dtThisMonth.Rows[row - 1]["I_DAY"]) + "</td>");
                            row++;
                        }
                        else
                        {
                            sb.Append("<td class='empty'></td>");
                        }
                    }
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            content1.InnerHtml = sb.ToString();



        }
        #endregion

        
    }
}